from PyQt5.QtWidgets import QDialog, QFormLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QHBoxLayout, QCheckBox
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt

from core.auth import AuthService
from database.db_session import get_db
from ui.main_window import MainWindow


class LoginDialog(QDialog):
    def __init__(self, app):
        super().__init__()
        self.app = app
        self.initUI()

    def initUI(self):
        self.setWindowTitle("用户登录")
        self.resize(500, 300)
        icon = QIcon("E:/pythonwork/personal_schedule_system/rsc/icon.jpg")
        self.setWindowIcon(icon)  # 设置窗口图标

        # 使用 QFormLayout 布局
        form_layout = QFormLayout()
        form_layout.setContentsMargins(50, 40, 50, 40)
        form_layout.setSpacing(15)

        # 创建用户名输入框
        self.username_input = QLineEdit()
        self.username_input.setPlaceholderText("请输入用户名")
        self.username_input.setMinimumHeight(40)
        form_layout.addRow(QLabel("用户名"), self.username_input)

        # 创建密码输入框
        self.password_input = QLineEdit()
        self.password_input.setPlaceholderText("请输入密码")
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.setMinimumHeight(40)
        form_layout.addRow(QLabel("密码"), self.password_input)

        # 添加显示密码的复选框
        self.show_password_checkbox = QCheckBox("显示密码")
        self.show_password_checkbox.setFont(QFont("微软雅黑", 11))
        self.show_password_checkbox.stateChanged.connect(self.toggle_password_visibility)
        form_layout.addRow(self.show_password_checkbox)

        # 创建按钮布局
        button_layout = QHBoxLayout()
        button_layout.setSpacing(15)

        self.login_btn = QPushButton("登录")
        self.login_btn.clicked.connect(self.try_login)
        self.login_btn.setMinimumHeight(45)
        self.login_btn.setStyleSheet("background-color: #4CAF50; color: white;")  # 设置按钮样式
        # 可以为登录按钮添加回车快捷键
        self.login_btn.setShortcut("Return")

        self.register_btn = QPushButton("去注册")
        self.register_btn.setMinimumHeight(45)
        self.register_btn.clicked.connect(self.open_register)
        self.register_btn.setStyleSheet("background-color: #8787ff; color: white;")

        button_layout.addWidget(self.login_btn)
        button_layout.addWidget(self.register_btn)

        # 将按钮布局添加到表单布局中
        form_layout.addRow(button_layout)

        # 设置整体布局
        self.setLayout(form_layout)

        # 设置样式表
        self.setStyleSheet("""
            QDialog {
                background-color: qlineargradient(
                    spread:pad, x1:0, y1:0, x2:1, y2:1,
                    stop:0 #e0f7fa, stop:1 #ffffff
                );
            }

            QLabel {
                font-family: "微软雅黑";
                color: #333333;
                font-size: 10pt;
            }
            
            /* 输入框样式 */
            QLineEdit {
                font-family: "Microsoft YaHei";
                font-size: 10pt;
                color: #333;
                background-color: #ffffff;
                border: 1px solid #cccccc;
                border-radius: 6px;
                padding: 6px;
            }
            
            /* 专门设置placeholder文字样式 */
            QLineEdit::placeholder {
                color: #95a5a6;
                font-style: italic;
            }

            QLineEdit:focus {
                border: 1px solid #0097FF;
                background-color: #e6f7ff;
            }

            QPushButton {
                color: white;
                border: none;
                border-radius: 6px;
                padding: 10px;
                font-weight: bold;
                font-size: 8pt;
                font-family: "Arial";
            }

            QPushButton:hover {
                background-color: #45a049;
            }

            QPushButton:pressed {
                background-color: #3e8e41;
            }
            
            /* 复选框样式 */
            QCheckBox {
                font-family: "微软雅黑";
                font-size: 8pt;
                color: #555;
                spacing: 8px;
            }
            
            QCheckBox::indicator {
                width: 18px;
                height: 18px;
            }
            
            QMessageBox {
                background-color: qlineargradient(
                    spread:pad, x1:0, y1:0, x2:1, y2:1,
                    stop:0 #e0f7fa, stop:1 #ffffff
                );
                border: 2px solid #C0C0C0; /* 边框样式 */
                border-radius: 5px; /* 圆角 */
                font-size: 8pt; /* 字体大小 */
            }

            QMessageBox QLabel {
                color: #333333; /* 文本颜色 */
                font-size: 10pt; /* 文本字体大小 */
            }
        """)

    def open_register(self):
        self.close()
        from ui.register_dialog import RegisterDialog
        self.register_window = RegisterDialog(self.app)
        self.register_window.show()

    def try_login(self):
        username = self.username_input.text().strip()
        password = self.password_input.text().strip()

        if not username or not password:
            QMessageBox.warning(self, "提示", "请输入用户名和密码")
            return

        try:
            db = next(get_db())
            result = AuthService.login(db, username, password)
        finally:
            db.close()  # 确保会话关闭
        if result:
            QMessageBox.information(self, "登录成功", f"欢迎 {username}")
            self.app.main_window = MainWindow(username)  # 将主窗口保存到应用程序对象中
            self.app.main_window.show()
            self.close()
        else:
            QMessageBox.critical(self, "登录失败", "用户名或密码错误")


    def toggle_password_visibility(self, state):
        if state == Qt.Checked:
            self.password_input.setEchoMode(QLineEdit.Normal)
        else:
            self.password_input.setEchoMode(QLineEdit.Password)

